package pony.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/PDFStoryServlet")
public class PDFStoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public PDFStoryServlet() {
        super();
    }

    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/pdf");
		ServletContext context = getServletContext();
		InputStream in = context.getResourceAsStream("/WEB-INF/res/dogAndCat.pdf");  
		// 取得指向WebApp中的pdf檔的輸入串流
		
		OutputStream out = response.getOutputStream(); // 取得輸出串流
		
		byte[] buffer = new byte[1024*3];  // 緩衝區大小為3M
		int length = 0;
		while((length = in.read(buffer)) != -1) {
			out.write(buffer, 0, length);
		}
		
		in.close();
		out.close();
		
	}

}
